package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired CatalogService catalogService;

    public void delete(String username){
        cartMapper.delete(username);
    }



    public void insertCart(String userid,String itemid){
        cartMapper.insertCart(userid,itemid);
    }

    public void updateCart(String userid, String itemid, int quantity){
        cartMapper.updateCart(userid,itemid,quantity);
    }


    public void removeCart(String userid, String itemid){
        cartMapper.removeCart(userid, itemid);
    }

    public Cart getCart(String userid){

      Cart cart=new Cart();
        List<CartItem> cartItemList=cartMapper.getCart(userid);
        Item item=new Item();
        for(int i=0;i<cartItemList.size ();i++){
            item=catalogService.getItem(cartItemList.get(i).getItemId());
            cart.addItem(item,true);
            cart.setQuantityByItemId(item.getItemId(),cartItemList.get(i).getQuantity());
        }

        return cart;
    }



}
